package com.baomidou.ant.controller;

import com.baomidou.ant.common.Result;
import com.baomidou.ant.entity.MyMusic;
import com.baomidou.ant.service.IMyMusicService;
import com.baomidou.ant.vo.Request.RequestUseMusicVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-03-24
 */
@RestController
@RequestMapping("/my-music")
public class MyMusicController {
    @Autowired
    private IMyMusicService myMusicService;

    @GetMapping("/listMyMusic/{userId}")
    public Result listMyMusic(@PathVariable Integer userId){
        QueryWrapper<MyMusic> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<MyMusic>  list=myMusicService.list(queryWrapper);
        return Result.getSuccess().setData(list);
    }


    @PostMapping("/useMusic")
    public Result useMusic(@RequestBody RequestUseMusicVo vo){
        //把之前使用的音乐换下
        QueryWrapper<MyMusic> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("is_use",1);
        queryWrapper.eq("user_id",vo.getUserId());
        MyMusic myMusic=myMusicService.getOne(queryWrapper);
        myMusic.setIsUse(0);
        //使用成功
        MyMusic music=myMusicService.getById(vo.getMusicId());
        music.setIsUse(1);
        myMusicService.saveOrUpdate(myMusic);
        myMusicService.saveOrUpdate(music);
        return  Result.getSuccess().setMsg("唱片更换成功");
    }

}
